package stuServlet;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class FindFileServlet extends HttpServlet {

	private String path;
	
	public FindFileServlet() {
		super();
	}

	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.execute(request, response);

	}
	public void execute(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException {
		List<String> list=new ArrayList<String>();
		String filename=getServletContext().getRealPath("")+path;
		File file=new File(filename);
		if(file.exists()){
			File []files=file.listFiles();
			for(File f:files){
				list.add(f.getPath().replace("\\", "/"));
			}
			request.setAttribute("list", list);
			request.getRequestDispatcher("stuJsp/download.jsp").forward(request, response);
		}
		
	}

	public void init() throws ServletException {
		path=getServletConfig().getInitParameter("path");
	}

}
